package org.catdroid.bomba.server.util;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.catdroid.bomba.server.Constants;

public class DatabaseUtil {

	public static Connection getConnection() {
		
		try {
	        InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup(Constants.DB_CONTEXT);
			return ds.getConnection();
			
		} catch (Exception e) {
			System.out.println("Error opening a Connection");
			throw new RuntimeException(e);
		}
	}
	
	public static void CloseResource (Connection connection) {
		if (connection!=null) {
			try {
				connection.close();
			}catch (Exception ex){ }
		}
	}
	public static void CloseResource (PreparedStatement prepared) {
		if (prepared!=null) {
			try {
				prepared.close();
			}catch (Exception ex){ }
		}
	}
}
